package com.ruoyi.project.WebMoudle.shopEmp;


import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.WebMoudle.ShopSuser.domain.ShopSuser;
import com.ruoyi.project.WebMoudle.ShopSuser.domain.ShopVo;
import com.ruoyi.project.WebMoudle.ShopSuser.service.IShopSuserService;
import com.ruoyi.project.WebMoudle.WxShop.domain.WxShop;
import com.ruoyi.project.WebMoudle.WxShop.service.IWxShopService;
import com.ruoyi.project.WebMoudle.WxUser.domain.WxUser;
import com.ruoyi.project.WebMoudle.WxUser.service.IWxUserService;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/emp/emp")
public class ShopEmpController extends BaseController {
    private String prefix = "shopEmp";
    @Autowired
    private IWxUserService wxUserService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IShopSuserService shopSuserService;
    @Autowired
    private IWxShopService wxShopService;


    @RequiresPermissions("emp:emp:view")
    @GetMapping()
    public String emp() {
        return prefix + "/emp";
    }


    @GetMapping("toAddVip/{userId}")
    public String toAddVip(@PathVariable("userId") Long userId, ModelMap mmap) {
        mmap.put("userId", userId);
        return prefix + "/toAddVip";
    }

    @RequiresPermissions("emp:emp:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(User user) {
        startPage();
        ShopSuser shopSuser = shopSuserService.selectOneShopId(getUserId());
        if (StringUtils.isNotNull(shopSuser)){
            WxShop wxShop = wxShopService.selectWxShopById(shopSuser.getShopId());
            ShopSuser ss = shopSuserService.selectEntityByWidAndSid(wxShop.getWxUserId(), wxShop.getId());
            user.setUserId(ss.getSysUserId());
        }
        List<User> list = userService.selectEmpList(user);
        return getDataTable(list);
    }

    @RequiresPermissions("emp:emp:add")
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<Role> list = roleService.queryMyEmpRole();
        mmap.put("roles", list);
        return prefix + "/add";
    }

    /*添加账号*/
    @RequiresPermissions("emp:emp:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated User user,Long wxUserId) {

        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(user.getLoginName()))) {
            return error("新增用户'" + user.getLoginName() + "'失败，登录账号已存在");
        }

        return toAjax(userService.addUSer(user,wxUserId));
    }

    /*单一删除后台账号*/
    @RequiresPermissions("emp:emp:remove")
    @PostMapping("/remove")
    @ResponseBody
    @Transactional
    public AjaxResult remove(Long ids) {
        ShopSuser shopSuser = shopSuserService.selectOneShopId(ids);
        WxUser wxUser = new WxUser();
        wxUser.setStatus("0");   //设置回会员状态
        wxUser.setId(shopSuser.getWxUserId());
        wxUserService.updateWxUser(wxUser);
        shopSuserService.deleteShopSuserById(shopSuser.getId());    //删除关联表
        userService.deleteUserById(ids);                             //删除系统用户
        return success();
    }


    /*根据手机号查询用户*/
    @RequestMapping(value = "/searchUser")
    @ResponseBody
    public Object searchUser(String phoneNum) {
        ShopSuser shopSuser = shopSuserService.selectOneShopId(getUserId());
        WxUser wxUser =  wxUserService.queryByPhone(phoneNum);
        if (StringUtils.isNotNull(wxUser)){    //存在此用户
            ShopVo ss = shopSuserService.queryByWidAndSid(wxUser.getId(),shopSuser.getShopId());
            if (StringUtils.isNotNull(ss)){      //已经是商家员工
                return   writeResultFailure("无需重复添加此员工");
            }
            return  writeResultRep(wxUser);
        }
        return writeResultFailure("无此用户");
    }

    /*绑定查看详情*/
    @RequestMapping(value = "/detail/{userId}")
    public String detail(@PathVariable("userId")Long userId ,ModelMap mmap) {
        User user = userService.selectUserById(userId);
        ShopSuser shopSuser = shopSuserService.selectOneShopId(userId);
        WxUser wxUser =new WxUser();
        if (StringUtils.isNotNull(shopSuser)){
            wxUser = wxUserService.selectWxUserById(shopSuser.getWxUserId());
        }
        mmap.put("user", user);
        mmap.put("wxUser", wxUser);
        mmap.put("roles", roleService.selectRolesByUserId(userId));
        return prefix + "/edit";
    }

    /*重置密码页面*/
    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Long userId, ModelMap mmap) {
        mmap.put("user", userService.selectUserById(userId));
        return "system/user/resetPwd";
    }

}
